package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.*;

public class BibliotecaApp {

    private Map<String, BibliotecaCommand> OPTIONS = new HashMap<String, BibliotecaCommand>();


    List<LibraryBook> bookList = new ArrayList<LibraryBook>();
    List<LibraryMovie> movieList = new ArrayList<LibraryMovie>();
    List<BibliotecaUser> userList = new ArrayList<BibliotecaUser>();

    private PrintStream printStream;
    private Scanner scanner;

    public BibliotecaApp(PrintStream printStream, Scanner scanner, BibliotecaUser bibliotecaUser) {
        this.printStream = printStream;
        this.scanner = scanner;

        this.OPTIONS.put("1", new BibliotecaBookList(this));
        this.OPTIONS.put("2", new BibliotecaBookReservations(this));
        this.OPTIONS.put("3", new BibliotecaMembershipDetails(this));
        this.OPTIONS.put("4", new BibliotecaMovieList(this));

        bookList.add(new LibraryBook("BOOK_ONE"));
        bookList.add(new LibraryBook("BOOK_TWO"));
        bookList.add(new LibraryBook("BOOK_THREE"));
        bookList.add(new LibraryBook("BOOK_FOUR"));
        bookList.add(new LibraryBook("BOOK_FIVE"));

        movieList.add(new LibraryMovie("JAWS", "8", "STEVEN SPIELBERG", "1975"));
        movieList.add(new LibraryMovie("THE SHAWSHANK REDEMPTION", "10", "FRANK DARABONT", "1994"));
        movieList.add(new LibraryMovie("THE SHINING", "7", "STANLEY KUBRICK", "1980"));
        movieList.add(new LibraryMovie("BIG MOMMA'S HOUSE", "3", "RAJA GOSNELL", "2000"));
        movieList.add(new LibraryMovie("MAN OF STEEL", "N/A", "ZAC SNYDER", "2013"));

        userList.add(new BibliotecaUser("111-1111", "1234", "Wole Solana",
                                        "osolana@thoughtworks.com", "012-345-6789"));
        userList.add(new BibliotecaUser("111-1112", "5678", "LeBron James",
                                        "kingjames23@miamiheat.com", "012-222-1234"));
    }

    public BibliotecaApp(PrintStream printStream, Scanner scanner, List<LibraryBook> books, List<LibraryMovie> movies,
                         List<BibliotecaUser> libraryUsers){

        this.printStream = printStream;
        this.scanner = scanner;
        this.bookList = books;
        this.movieList = movies;
        this.userList = libraryUsers;

        this.OPTIONS.put("1", new BibliotecaBookList(this));
        this.OPTIONS.put("2", new BibliotecaBookReservations(this));
        this.OPTIONS.put("3", new BibliotecaMembershipDetails(this));
        this.OPTIONS.put("4", new BibliotecaMovieList(this));
    }

    public void outputMessage(String message){
        printStream.println(message);
    }

    public void welcomeMessage() {
        printStream.println("Welcome to this awesome Library!\n");
    }

    public void displayMenu() {
        printStream.println("****** Options ******\n" +
                "1. View List of Books\n" +
                "2. Reserve a book\n" +
                "3. Check membership details\n" +
                "4. View movie collection");
    }

    public List<LibraryBook> getBookList(){
        return bookList;
    }

    public List<LibraryMovie> getMovieList(){
        return movieList;
    }

    public List<BibliotecaUser> getUserList() {
        return userList;
    }

    public String getInput(){
        return scanner.nextLine();
    }
    
    public String [] getUserCredentials(){
        printStream.println("Enter your user login: ");
        String userLogin = this.getInput();
        printStream.println("Enter your user password: ");
        String userPassword = this.getInput();
        String[] userCredentials = {userLogin, userPassword};
        return userCredentials;
    }


    public void selectOption() {
        String choice = getInput();
        if (OPTIONS.containsKey(choice) && (choice.equals("1") || choice.equals("4"))) {
            OPTIONS.get(choice).execute();
        }

        else if (OPTIONS.containsKey(choice) && (choice.equals("2") || choice.equals("3"))) {
            OPTIONS.get(choice).execute();
        }

        else {
            outputMessage("Select a valid option!!");
        }
    }

}
